package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessQandA;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AnswerVO;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.text.MessageFormat;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017/1/11 17:00
 * 修改备注：
 * ================================================
 */
public class AnswerListHolder extends BaseViewHolder<AnswerVO> {

    private TextView tvName;
    private TextView tvContent;
    private TextView tvPraise;
    private TextView tvShare;
    private TextView tvDel;
    private TextView tvDate;
    private TextView tvMyQus;

    private RecyclerArrayAdapter mAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG.MSG_DELETE_ANSWER_SUCCESS:
                    mAdapter.remove(getDataPosition());
                    ToastUtil.showToast(getContext(), "删除成功");
                    break;
                case MSG.MSG_DELETE_ANSWER_FIELD:
                    ToastUtil.showToast(getContext(), msg.obj.toString());
                    break;
                case MSG.MSG_FETCH_ANSWER_SHARE_DATA_SUCCESS:
                    String shareImg = FastJsonUtil.getString(msg.obj.toString(), "imagepath");
                    String shareTitle = FastJsonUtil.getString(msg.obj.toString(), "title");
                    String shareIntroduce = FastJsonUtil.getString(msg.obj.toString(), "introduce");
                    String shareUrl = FastJsonUtil.getString(msg.obj.toString(), "url");
                    shareQuestionAnswer(shareImg, shareTitle, shareIntroduce, shareUrl);
                    break;
                case MSG.MSG_FETCH_ANSWER_SHARE_DATA_FIELD:
                    ToastUtil.showToast(getContext(), msg.obj.toString());
                    break;
            }
        }
    };

    /**
     * 分享问题答案
     */
    private void shareQuestionAnswer(String shareImg, String shareTitle, String shareIntroduce, String shareUrl) {
//        ShareUtil.showShare((Activity) getContext(), shareTitle, shareIntroduce, shareUrl, shareImg);
    }

    public AnswerListHolder(ViewGroup parent) {
        super(parent, R.layout.item_answer_list);
        tvName = $(R.id.answer_list_tv_name);
        tvContent = $(R.id.answer_list_tv_content);
        tvPraise = $(R.id.answer_list_tv_praise);
        tvShare = $(R.id.answer_list_tv_share);
        tvDel = $(R.id.answer_list_tv_delete);
        tvDate = $(R.id.answer_list_tv_date);
        tvMyQus = $(R.id.answer_list_tv_my_qus);
    }

    @Override
    public void setData(final AnswerVO data) {
        super.setData(data);
        mAdapter = getOwnerAdapter();
        tvName.setText(data.getPerson() != null ? !StringUtil.isStrEmpty(data.getPerson().getNiceName()) ? data.getPerson().getNiceName() : data.getPerson().getUserName() : "匿名");
        tvContent.setText(!StringUtil.isStrEmpty(data.getWordContent()) ? StringUtil.stripHtml(data.getWordContent()) : "点击查看详情");
        tvPraise.setText(MessageFormat.format("{0}赞同", data.getPraiseCount()));
        tvDate.setText(DateUtil.StringToString(data.getCreateTime(), DateStyle.YYYY_MM_DD_EN_HH_MM_EN));

        if (data.getPerson().getUserAccId().equals(UserXML.getUserId(getContext())))
            tvMyQus.setVisibility(View.VISIBLE);
        else
            tvMyQus.setVisibility(View.GONE);

        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BusinessTechnology.fetchAnswerShareData((Activity) getContext(), data.getId(), handler);
            }
        });

        tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BusinessQandA.deleteAnswer((Activity) getContext(), data.getId(), handler);
            }
        });


    }
}
